package com.example.composemodifiterdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 *@author: njb
 *@date:  2023/2/9 23:17
 *@desc:
 */
class CountSampleActivity:ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            CounterComponent()
        }
    }

    @Preview
    @Composable
    fun CounterComponent() {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            var counter by remember { mutableStateOf(0) }

            Text( //1
                "Click the buttons to adjust your value:",
                Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center
            )

            Text( //2
                "$counter",
                Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center,
                style = MaterialTheme.typography.h3
            )

            Row {
                Button(
                    onClick = { counter-- },
                    Modifier.weight(1f)
                ) {
                    Text("-")
                }
                Spacer(Modifier.width(16.dp))
                Button(
                    onClick = { counter++ },
                    Modifier.weight(1f)
                ) {
                    Text("+")
                }
            }
        }
    }
}